import 'package:get/get.dart';
import 'package:safetymonitor/app/core/core.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/core/utils/user_manager.dart';
import 'package:safetymonitor/app/data/entities/application/filter/filter_bean.dart';
import 'package:safetymonitor/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:safetymonitor/app/data/entities/home/warning/get_all_device_type_entity.dart';
import 'package:safetymonitor/modules/home/new_alarm_warning_page/completed/controller.dart';
import 'package:safetymonitor/modules/home/new_alarm_warning_page/device_completed/controller.dart';

class NewAlarmWarningPageController extends GetxController {
  bool selectButtonFirst = true;
  bool selectButtonSecond = false;
  bool selectButtonThird = false;
  bool selectButtonFourth = false;
  int index = 1;

  ///报警预警的=1  设备=3
  int dataType = 1;

  /// 更新信息
  void updateInfo() {
    update();
  }

  /// 筛选列表
  List<FilterBean> filterList = [];
  String? company;
  String? level;
  String monitorCode = '';
  List<FilterBean> filterList2 = [];
  String? company2;
  String? level2;
  String monitorCode2 = '';

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 右侧边栏
  final GlobalKey<ScaffoldState> rightScaffoldKey = GlobalKey();

  /// 初始化数据
  Future<void> initData() async {
    /// 只有总公司才有
    if (UserManager.getUserCompanyId() == 100) {
      final List<FilterEntitiesBean> company = [];
      // 添加状态筛选条件
      company.add(FilterEntitiesBean(name: '五洲矿业 ', value: '115'));
      company.add(FilterEntitiesBean(name: '化学分公司', value: '118'));
      company.add(FilterEntitiesBean(name: '冶炼分公司', value: '119'));
      company.add(FilterEntitiesBean(name: '金属分公司', value: '120'));
      company.add(FilterEntitiesBean(name: '矿山分公司', value: '121'));
      company.add(FilterEntitiesBean(name: '金钼汝阳', value: '122'));
      //筛选控制bean
      final FilterBean filterBean = FilterBean(title: '单位', filterList: company);
      filterList.add(filterBean);
      filterList2.add(filterBean);
    }

    final List<FilterEntitiesBean> level = [];
    // 添加状态筛选条件
    level.add(FilterEntitiesBean(name: '红色', value: '1'));
    level.add(FilterEntitiesBean(name: '橙色', value: '2'));
    level.add(FilterEntitiesBean(name: '黄色', value: '3'));
    level.add(FilterEntitiesBean(name: '蓝色', value: '4'));
    //筛选控制bean
    final FilterBean filterBean2 = FilterBean(title: '等级', filterList: level);
    filterList.add(filterBean2);
    filterList2.add(filterBean2);

    final List<GetAllDeviceTypeEntity> monitorCodeList = await getAllDeviceType('${UserManager.getUserCompanyId()}');

    final List<FilterEntitiesBean> monitorCode = [];
    for (var item in monitorCodeList) {
      monitorCode.add(FilterEntitiesBean(name: item.getShowName(), value: item.getId()));
    }
    final FilterBean filterBean3 = FilterBean(title: '系统-类别', filterList: monitorCode);
    filterList.add(filterBean3);
    filterList2.add(filterBean3);
  }

  /// 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    company = null;
    level = null;
    monitorCode = '';
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '单位') {
        company = i.chosenValue[0].value as String;
      }
      if (i.title == '等级') {
        level = i.chosenValue[0].value as String;
      }
      if (i.title == '系统-类别') {
        monitorCode = i.chosenValue[0].value as String;
      }
    }
    Get.find<DeviceOffLineCompletedController>().globalKey.currentState?.onRefresh();
    update();
  }
 /// 筛选确认
  void screenConfirm2(List<FilterBean> value) {
    filterList2 = value;
    company2 = null;
    level2 = null;
    monitorCode2 = '';
    for (var i in value) {
      if (i.chosenValue.isEmpty) {
        continue;
      }
      if (i.title == '单位') {
        company2 = i.chosenValue[0].value as String;
      }
      if (i.title == '等级') {
        level2 = i.chosenValue[0].value as String;
      }
      if (i.title == '系统-类别') {
        monitorCode2 = i.chosenValue[0].value as String;
      }
    }
    Get.find<CompletedController>().globalKey.currentState?.onRefresh();
    update();
  }

  void onFilterButtonPressed() {
    rightScaffoldKey.currentState!.openEndDrawer();
  }

  ///获取所有的检测项
  Future<List<GetAllDeviceTypeEntity>> getAllDeviceType(String userCompanyId,{bool isShowLoading = false}) async {
    final res = await HttpUtil().generateGetRequest<List<GetAllDeviceTypeEntity>>(
      Interface.getAllDeviceType,
      queryParameters: {
        'companyId': userCompanyId,
      },
    ).execute(isShowLoading:isShowLoading);
    List<GetAllDeviceTypeEntity> dataList = [];
    res.onSuccess((result) {
      if (result.data != null) {
        dataList = result.data!;
      }
    });
    return dataList;
  }
}
